<template>
    <div class="gonggao-add" v-loading="loading">
        <el-card class="box-card">
            <div slot="header" class="clearfix updt">
                <el-page-header @back="$router.go(-1)" content="编辑公告"> </el-page-header>
            </div>
            <div class="form-database-form">
                <el-form :model="form" ref="formModel" label-width="130px" status-icon validate-on-rule-change>
                    <el-form-item label="公告编号" prop="gonggaobianhao" :rules="[{required:true, message:'请填写公告编号'}]">
                        <el-input type="text" placeholder="输入公告编号" style="width: 250px" v-model="form.gonggaobianhao" />
                    </el-form-item>

                    <el-form-item
                        label="公告标题"
                        prop="gonggaobiaoti"
                        required
                        :rules="[{required:true, message:'请填写公告标题'}, {validator:rule.checkRemote, message:'内容重复了', checktype:'update', module:'gonggao', col:'gonggaobiaoti', id:form.id, trigger:'blur'}]"
                    >
                        <el-input type="text" placeholder="输入公告标题" style="width: 250px" v-model="form.gonggaobiaoti" />
                    </el-form-item>

                    <el-form-item label="公告图片" prop="gonggaotupian"> <e-upload-image v-model="form.gonggaotupian"></e-upload-image> </el-form-item>

                    <el-form-item label="公告内容" prop="gonggaoneirong"> <e-editor v-model="form.gonggaoneirong"></e-editor> </el-form-item>

                    <el-form-item label="发布人" prop="faburen"> <el-input v-model="form.faburen" readonly style="width: 250px"></el-input> </el-form-item>

                    <el-form-item v-if="btnText">
                        <el-button type="primary" @click="submit">{{ btnText }}</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </el-card>
    </div>
</template>
<style type="text/scss" scoped lang="scss">
    .gonggao-add {
    }
</style>
<script>
    import api from "@/api";
    import rule from "@/utils/rule";
    import { extend } from "@/utils/extend";

    export default {
        name: "gonggao-add",
        data() {
            return {
                rule,
                loading: false,
                form: {
                    gonggaobianhao: rule.getID(),
                    gonggaobiaoti: "",
                    gonggaotupian: "",
                    gonggaoneirong: "",
                    faburen: this.$store.state.user.session.username,
                },
            };
        },
        watch: {
            id: {
                handler() {
                    this.loadInfo();
                },
            },
        },
        props: {
            isRead: {
                type: Boolean,
                default: true,
            },
            btnText: {
                type: String,
                default: "提交",
            },
            id: {
                type: [String, Number],
                required: true,
            },
        },

        computed: {},
        methods: {
            submit() {
                this.$refs.formModel
                    .validate()
                    .then((res) => {
                        if (this.loading) return;
                        this.loading = true;
                        var form = this.form;

                        this.$post(api.gonggao.update, form)
                            .then((res) => {
                                this.loading = false;
                                if (res.code == api.code.OK) {
                                    this.$message.success("添加成功");
                                    this.$emit("success", res.data);
                                    this.$refs.formModel.resetFields();
                                    this.loadInfo();
                                } else {
                                    this.$message.error(res.msg);
                                }
                            })
                            .catch((err) => {
                                this.loading = false;
                                this.$message.error(err.message);
                            });
                    })
                    .catch((err) => {
                        console.log(err.message);
                    });
            },
            loadInfo() {
                // 更新数据,获取数据
                this.loading = true;
                var form = this.form;
                this.$post(api.gonggao.edit, {
                    id: this.id,
                })
                    .then((res) => {
                        this.loading = false;
                        if (res.code == api.code.OK) {
                            extend(this, res.data);
                            this.form = res.data.mmm;
                        } else {
                            this.$message.error(res.msg);
                            this.$router.go(-1);
                        }
                    })
                    .catch((err) => {
                        this.$message.error(err.message);
                        this.$router.go(-1);
                    });
            },
        },
        created() {
            this.loadInfo();
        },
        mounted() {},
        destroyed() {},
    };
</script>
